'use strict';

const BaseService = require('./base');

class HouseInfoService extends BaseService {
  get _model() {
    return 'HouseInfo';
  }

  async findOne({ id }) {
    const { app } = this;
    const where = { id };
    const include = [
      {
        model: app.model.House,
        attributes: {
          exclude: app.exclude,
        },
        include: [
          {
            model: app.model.User,
            as: 'Owner',
            attributes: {
              exclude: app.exclude.concat([ 'password' ]),
            },
          },
        ],
      },
    ];
    return this._findOne({ where, include });
  }

  async create({ houseId, status }) {
    return this._create({
      houseId, status,
    });
  }

  async update({ content, pictures, houseArea, direction, floor, type, remarks, personNum, deviceIds, advantageIds, status, id }) {
    const where = { id };
    const body = { content, pictures, houseArea, direction, floor, type, remarks, personNum, deviceIds, advantageIds, status };
    return this._update({ body, where });
  }

  async destory(houseId) {
    const where = { houseId };
    return this._destory({ where });
  }

  async findList({ houseArea, direction, floor, type, personNum, status, oid, pageIndex, pageSize, createdAt, prop, sort }) {
    const { app } = this;
    const where = this.formatWhere({ dim: { direction, floor, type }, range: { createdAt, personNum, houseArea }, exact: { status } });
    const whereHouse = this.formatWhere({ exact: { oid } });
    const order = this.formatOrder(prop, sort);
    const include = [
      {
        model: app.model.House,
        attributes: [ 'name' ],
        where: whereHouse,
        include: [
          {
            model: app.model.User,
            as: 'Owner',
            attributes: [ 'nickName' ],
          },
          {
            model: app.model.Area,
            attributes: [ 'name' ],
          },
        ],
      },
    ];
    return this._findList({ pageIndex, pageSize, where, order, include });
  }
}

module.exports = HouseInfoService;
